|
|
|
|
|
|
|
Do and Exit For statements used to end a loop early. The following code shows how the Exit Sub statement is used to exit a routine before reaching the end of the routine: |
|
|
|
|
|
|
|
|
Public Sub Get_Input()
Dim Data As Integer
Data = InputBox(Enter a Number, App.Title)
If Data = 0 Then
MsgBox Zero Entered, Process Cancelled
Exit Sub
End If
MsgBox The number entered was: & Data
End Sub |
|
|
|
|
|
|
|
|
When working with subroutines, you can see that even though they can accept parameters, they return no values other than any modifications they might make to the variables that were passed. To create a subroutine that can return a value that is not a modification of a parameter that was passed, you would use a special form of the subroutine called a function. The only difference between a function and a subroutine is that a function can return a value to the calling routine. Both functions and subroutines can have variables passed to them, and they both accept the same types of arguments. The syntax of a function is only slightly different from a subroutine, and is shown here: |
|
|
|
|
|
|
|
|
[Public ¦ Private ¦ Friend] [Static] Function name [(arglist)] [As type]
[statements]
[name = expression]
End Function |
|
|
|
|
|
|
|
|
To return a value from a function, you only need to assign the value to the function name as in the following: |
|
|
|
|
|
|
|
|
Public Function CalcTotal() as Single
[Function Code]
CalcTotal = Sale_Amount
End Function |
|
|
|
|
|
|
|
|
This code would return the final sale amount to the calling procedure. You would call this routine as in the following line of code: |
|
|
|
|
|
|
|
|
Invoice_Line_Amt = CalcTotal |
|
|
|
|
|
|
|
|
If you are passing arguments to the function, the calling statement would look like this: |
|
|
|
|
|
|
|
|
Invoice_Line_Amt = CalcTotal(Qty, Price) |
|
|
|
|
|
|
|
|
Again, if you must exit the function before the entire routine is executed, you would use an Exit function. |
|
|
|
|
|